<?php

require_once 'class.history.php';
require_once $GLOBALS['ROO'] . 'Modules/CartaoPontos/class.cartaopontos.php';

class Remittance {

    function NewRemittance() {

    }

    function EditRemittance($toChange) {
        global $sysTime, $SESSID;
        # 0010 Alter Invoice
        hs_invoiceextend($toChange['FraID'], $sysTime);
        if (true == true) {
            $GLOBALS['MYT']->db_query("UPDATE facturasextjoins
		SET
			FraMTCN = '{$toChange['FraMTCN']}'
		WHERE FraExtID = {$toChange['FraID']}");
            /* $GLOBALS['MYT']->db_query("UPDATE facturasextjoins
              SET
              FraCliNombre = '{$toChange['CliNombre']}',
              FraCliDomicilio = '{$toChange['CliDomicilio']}',
              FraCliPortal = '{$toChange['CliPortal']}',
              FraBnfNombre = '{$toChange['BnfNombre']}',
              FraBnfDireccion = '{$toChange['BnfDireccion']}',
              FraBnfCedula = '{$toChange['BnfCedula']}',
              FraCliPisoPuerta = '{$toChange['CliPisoPuerta']}',
              FraCliLocation = '{$toChange['CliLocation']}',
              FraCliCP = '{$toChange['CliCP']}',
              FraCliNacionalidad = {$toChange['CliNacionalidad']},
              FraCliTelefono = '{$toChange['CliTelefono']}',
              FraCliTelefono2 = '{$toChange['CliTelefono2']}',
              FraCliCiudad = {$toChange['CliCiudad']},
              FraCliObs = '{$toChange['ClObs']}',
              FraBnfCiudad = '{$toChange['BnfCiudad']}',
              FraBnfTelefono = '{$toChange['BnfTelefono']}',
              FraBnfPais = {$toChange['BnfPais']},
              FraBnfBarrio = '{$toChange['BnfBarrio']}',
              FraCuenta = '{$toChange['cb_Cuenta']}',
              FraCuenta_prx = '{$toChange['cb_Cuenta_prx']}',
              FraCuenta_sux = '{$toChange['cb_Cuenta_sux']}',
              FraCuenta_mod = '{$toChange['cb_Cuenta_mod']}',
              FraSucursal = '{$toChange['cb_Sucursal']}',
              FraCiudadSucursal = '{$toChange['cb_CiudadSucursal']}',
              FraTipoCuenta = {$toChange['cb_TipoCuenta']},
              FraBancoID = {$toChange['cb_Banco']},
              FraCashierCorr = {$toChange['FraCorresponsal']},
              FraBnfDocType = {$toChange['BnfDocType']}

              WHERE FraExtID = {$toChange['FraID']}"); */
            #if($GLOBALS['MYT']->db_query("UPDATE facturas SET FraStatus = {$toChange['FraStatus']}
            #	WHERE FraID = {$toChange['FraID']}")) {
            return $toChange['FraID'];
        }
    }

    function CancelRemittance($RemittanceID, $PIDCODE, $NewStatus, $ActionObservation) {
        global $sysTime, $SESSID, $SESSCD, $SESSIP;
        $ViewFra = $GLOBALS['MYT']->db_array("SELECT * FROM facturas WHERE FraID = $RemittanceID");

        if ($GLOBALS['MYT']->db_query("UPDATE facturas SET FraStatus = {$NewStatus}, FraFecha = IF(FraModo=2,'$sysTime',FraFecha) WHERE FraID = $RemittanceID") == true) {
            if ($ViewFra[1]['FraModo'] == 1) {
                if (($NewStatus == 9 || $NewStatus == 1) && $ViewFra[1]['FraStatus'] != 9 && $ViewFra[1]['FraStatus'] != 1) {
                    #Repoe valor de ultimo envio
                    $GetExtCli = $GLOBALS['MYT']->db_array("SELECT CliHasArc FROM clientes_ext WHERE CliExtNumero = {$ViewFra[1]['FraCliNumero']}");
                    $GetNewLast = $GLOBALS['MYT']->db_array("SELECT MAX(FraFechaIngreso) AS FraFechaIngreso, FraID FROM facturas WHERE FraCliNumero = {$ViewFra[1]['FraCliNumero']} AND FraStatus NOT IN (1,9,10,21)");
                    if ($GetNewLast[1]['FraFechaIngreso'] == '' AND $GetExtCli[1]['CliHasArc'] == 1) {
                        $GetNewLast = $GLOBALS['MYT']->db_array("SELECT MAX(FraFechaIngreso) AS FraFechaIngreso, FraID FROM officeschema_stored.facturas WHERE FraCliNumero = {$ViewFra[1]['FraCliNumero']} AND FraStatus NOT IN (1,9,10,21)");
                    }
                    if ($GetNewLast[1]['FraFechaIngreso'] == '') {
                        $GetNewLast[1]['FraFechaIngreso'] = 'null';
                        $GetNewLast[1]['FraID'] = '0';
                    } else {
                        $GetNewLast[1]['FraFechaIngreso'] = "'" . $GetNewLast[1]['FraFechaIngreso'] . "'";
                    }
                    $GLOBALS['MYT']->db_query("UPDATE clientes_ext SET CliLastSend = {$GetNewLast[1]['FraFechaIngreso']}, numerofactura = {$GetNewLast[1]['FraID']} WHERE CliExtNumero = {$ViewFra[1]['FraCliNumero']}");
                    ## Fim repoe data de ultimo envio
                    $GetBonus = $GLOBALS['MYT']->db_array("SELECT * FROM bonusinvoice WHERE bns_Fra = $RemittanceID AND bns_Type IN (1,7,11)");
                    for ($r = 1; $r <= $GetBonus[0]; $r++) {
                        if ($GetBonus[$r]['CAMPAIGN'] == 2 && $GetBonus[$r]['bns_Type'] == 7) {
                            $GLOBALS['MYT']->db_query("UPDATE unionclientesangariadores SET uca_Flag = 1 WHERE uca_Gather = {$GetBonus[$r]['CLIENT']}");
                        }
                        $MkBalance = make_bnsbalance(0, $GetBonus[$r]['bns_Amount'], $GetBonus[$r]['CLIENT']);
                        $GLOBALS['MYT']->db_query("INSERT INTO bonusinvoice (INVOICE, CLIENT, CAMPAIGN, bns_Time, bns_User, bns_Amount, bns_Type, bns_CliReference, bns_Origen1, bns_Currency, bns_Value, bns_Product, bns_Status, bns_Partner, bns_Fra, bns_Balance)
			VALUES (0,{$GetBonus[$r]['CLIENT']},1,'{$sysTime}',{$_SESSION[$SESSID]},-{$GetBonus[$r]['bns_Amount']},2,0,{$_SESSION[$SESSCD]},1,-{$GetBonus[$r]['bns_Value']},0,2,0,{$GetBonus[$r]['bns_Fra']},{$MkBalance})");
                    }
                }
                if ($NewStatus == 9 && $ViewFra[1]['FraStatus'] != 1 && $ViewFra[1]['FraStatus'] != 9 && $ViewFra[1]['FraAlbaran'] > 0 || $NewStatus == 1 && $ViewFra[1]['FraStatus'] != 1 && $ViewFra[1]['FraStatus'] != 9 && $ViewFra[1]['FraAlbaran'] > 0) {
                    if ($ViewFra[1]['FraOrigen2'] == '') {
                        $ViewFra[1]['FraOrigen2'] = 0;
                    }
                    if ($ViewFra[1]['FraOrdenPago'] == '') {
                        $ViewFra[1]['FraOrdenPago'] = 0;
                    }
                    if ($ViewFra[1]['FraCtaBenef'] == '') {
                        $ViewFra[1]['FraCtaBenef'] = 0;
                    }
                    if ($ViewFra[1]['FraNumero'] > 0 && $PIDCODE == 21) {
                        $est_cargo = 0;
                        $est_cargousd = 0;
                        $est_ComisionAg = 0;
                        $est_ComisionCorr = 0;
                    } else {
                        $est_cargo = 0 - $ViewFra[1]['FraCargo'];
                        $est_cargousd = 0 - $ViewFra[1]['FraCargoUSD'];
                        $est_ComisionAg = 0 - $ViewFra[1]['FraComisionAg'];
                        $est_ComisionCorr = 0 - $ViewFra[1]['FraComisionCorr'];
                    }
                    $est_fecha = "'" . $sysTime . "'";
                    $est_fechaalb = "'" . $sysTime . "'";
                    if ($ViewFra[1]['FraNumero'] == 0) {
                        $est_fecha = 'null';
                    }
                    if ($ViewFra[1]['FraAlbaran'] == 0) {
                        $est_fechaalb = 'null';
                    }

                    $GLOBALS['MYT']->db_query("INSERT
		INTO facturas
		(FraFecha, FraFechaAlb, FraFechaIngreso,
			FraUsuario,
			FraCorresponsal,
			FraAgenciaPago,
			FraReferenciaAg,
			FraRefAux,
			FraReferenciaCo,
			FraOrdenPago,
			FraComisionAg,
			FraComisionAgR,
			FraRetencion,
			FraComisionComercial,
			FraComisionCorr,
			FraOrigen0,
			FraOrigen1,
			FraOrigen2,
			FraOrigen3,
			FraCliNumero,
			FraBnfNumero,
			FraCtaBenef,
			FraMonCobro,
			FraEnviado,
			FraCargo,
			FraEnviadoUSD,
			FraCargoUSD,
			FraUrgente,
			FraMonDPago,
			FraARecibir,
			FraDolares,
			FraCambio,
			FraTasaAgn,
			FraTasa,
			FraMensaje,
			FraNotas,
			FraClave,
			FraConcepto,
			FraDifCambio,
			FraStatus,
			FraProm,
			FraModoPago)
		VALUES
		({$est_fecha},{$est_fechaalb},'{$sysTime}',{$_SESSION[$SESSID]},{$ViewFra[1]['FraCorresponsal']},{$ViewFra[1]['FraAgenciaPago']},{$ViewFra[1]['FraReferenciaAg']},
		{$ViewFra[1]['FraRefAux']},{$ViewFra[1]['FraReferenciaCo']},{$ViewFra[1]['FraOrdenPago']},{$est_ComisionAg},{$ViewFra[1]['FraComisionAgR']},
		{$ViewFra[1]['FraRetencion']},{$ViewFra[1]['FraComisionComercial']},{$est_ComisionCorr},{$ViewFra[1]['FraOrigen0']},{$ViewFra[1]['FraOrigen1']},{$ViewFra[1]['FraOrigen2']},'{$_SESSION[$SESSIP]}',
		'{$ViewFra[1]['FraCliNumero']}',{$ViewFra[1]['FraBnfNumero']},{$ViewFra[1]['FraCtaBenef']},'{$ViewFra[1]['FraMonCobro']}',-{$ViewFra[1]['FraEnviado']},$est_cargo,
		-{$ViewFra[1]['FraEnviadoUSD']},$est_cargousd,{$ViewFra[1]['FraUrgente']},'{$ViewFra[1]['FraMonDPago']}',-{$ViewFra[1]['FraARecibir']},
		-{$ViewFra[1]['FraDolares']},{$ViewFra[1]['FraCambio']},{$ViewFra[1]['FraTasaAgn']},{$ViewFra[1]['FraTasa']},'','',
		'{$ViewFra[1]['FraClave']}',{$ViewFra[1]['FraConcepto']},{$ViewFra[1]['FraDifCambio']},10,-{$ViewFra[1]['FraProm']},{$ViewFra[1]['FraModoPago']})");

                    if ($ViewFra[1]['FraProm'] > 0) {
                        $ChkCashier = $GLOBALS['MYT']->db_array("SELECT * FROM csh_moviments
					INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
					WHERE cvu_OpType = CorrCashier AND cvu_Type = 5 AND cvu_Office = {$ViewFra[1]['FraOrigen0']} AND cvu_User = {$ViewFra[1]['FraOrigen2']} AND cvu_Status = 1
											AND cvu_Currency = {$ViewFra[1]['FraMonCobro']}
											AND DATE(cvu_Time) = DATE('{$sysTime}')");
                        if (isset($ChkCashier[1]['cvu_ID'])) {
                            $GLOBALS['MYT']->db_query("UPDATE csh_moviments
					INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
					SET cvu_Amount = cvu_Amount-{$ViewFra[1]['FraProm']}
					WHERE cvu_ID = {$ChkCashier[1]['cvu_ID']}");
                        } else {
                            $GETCASHIER = $GLOBALS['MYT']->db_array("SELECT CorrCashier FROM corresponsales WHERE CorrNumero = {$ViewFra[1]['FraCorresponsal']}");
                            $GLOBALS['MYT']->db_query("INSERT INTO csh_moviments
			(cvu_Type, cvu_User, cvu_Office, cvu_Time, cvu_Status, cvu_POOL, cvu_Amount, cvu_OpType, cvu_Currency, cvu_Deposit) VALUES (5,{$_SESSION[$SESSID]},{$ViewFra[1]['FraOrigen0']},'{$sysTime}',1,0,0-{$ViewFra[1]['FraProm']},{$GETCASHIER[1]['CorrCashier']},{$ViewFra[1]['FraMonCobro']},0)");
                        }
                    }
                }
                # Cancela promocoes para remessas sem transmissao de dominio
                elseif ($NewStatus == 1 && $ViewFra[1]['FraStatus'] != 1 && $ViewFra[1]['FraStatus'] != 9 && $ViewFra[1]['FraAlbaran'] == 0) {
                    if ($ViewFra[1]['FraProm'] > 0) {
                        $ChkCashier = $GLOBALS['MYT']->db_array("SELECT * FROM csh_moviments
					INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
					WHERE cvu_OpType = CorrCashier AND cvu_Type = 5 AND cvu_Office = {$ViewFra[1]['FraOrigen0']} AND cvu_User = {$ViewFra[1]['FraOrigen2']} AND cvu_Status = 1
											AND cvu_Currency = {$ViewFra[1]['FraMonCobro']}
											AND DATE(cvu_Time) = DATE('{$ViewFra[1]['FraFechaIngreso']}')");
                        if (isset($ChkCashier[1]['cvu_ID'])) {
                            $GLOBALS['MYT']->db_query("UPDATE csh_moviments
					INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
					SET cvu_Amount = cvu_Amount-{$ViewFra[1]['FraProm']}
					WHERE cvu_ID = {$ChkCashier[1]['cvu_ID']}");
                        } else {
                            $GETCASHIER = $GLOBALS['MYT']->db_array("SELECT CorrCashier FROM corresponsales WHERE CorrNumero = {$ViewFra[1]['FraCorresponsal']}");
                            $GLOBALS['MYT']->db_query("INSERT INTO csh_moviments
			(cvu_Type, cvu_User, cvu_Office, cvu_Time, cvu_Status, cvu_POOL, cvu_Amount, cvu_OpType, cvu_Currency, cvu_Deposit) VALUES (5,{$ViewFra[1]['FraOrigen2']},{$ViewFra[1]['FraOrigen0']},'{$ViewFra[1]['FraFechaIngreso']}',1,0,0-{$ViewFra[1]['FraProm']},{$GETCASHIER[1]['CorrCashier']},{$ViewFra[1]['FraMonCobro']},0)");
                        }
                    }
                }
            } else {
                if ($NewStatus == 18) {
                    $row = tuga_multiquery("CALL fn_incidence (@IncidentID,$RemittanceID,'{$sysTime}',{$_SESSION[$SESSID]},'{$ActionObservation}',2,'FraStatus',{$ViewFra[1]['FraStatus']},'{$NewStatus}',1,null); SELECT @IncidentID");
                }
            }
            hs_invoicestatus($RemittanceID, $sysTime, 7, $NewStatus, $ActionObservation, $sysTime);
        } else {
            $error = "Dados incorrectos.";
        }
        if (!isset($error)) {
            return true;
        }
    }

    static function GetAction($ob_pos) {

        if ($ob_pos < 1) {
            $ob_pos = 2;
        }
        if ($ob_pos == 1 || $ob_pos == 9) {
            $GetSt = $ob_pos;
        } elseif ($ob_pos == 14) {
            $GetSt = '1,2,14';
        } elseif ($ob_pos > 6) {
            $GetSt = '7,8,9,11,12,13,14,15,20';
        } else {
            $GetSt = $ob_pos . ',1,9';
        }
        $Qur = "SELECT sg_ID AS ID, CONCAT(sg_Abreviatura,' | ',sg_Descripcion) AS NAME
				FROM hd_statusfacturas
				WHERE sg_ID IN ({$GetSt})";
        mn_field('select', 'ED', 'FraStatus', $ob_pos, $Qur, '', '', '');
    }

    function ActionRemittance($RemittanceID, $PIDCODE, $NewStatus, $ActionObservation) {
        global $sysTime, $SESSID, $SESSCD, $SESSIP;
        $ViewFra = $GLOBALS['MYT']->db_array("SELECT *, DATE(FraFechaIngreso) AS ValDate FROM facturas WHERE FraID = $RemittanceID");

        if ($PIDCODE == 2) {
            if ($ViewFra[1]['FraFechaAlb'] == '') {
                if ($GLOBALS['MYT']->db_query("UPDATE facturas SET
				FraStatus = '$NewStatus'
					WHERE FraID = $RemittanceID") == true) {
                    if ($NewStatus == 1) {
                        $GetExtCli = $GLOBALS['MYT']->db_array("SELECT CliHasArc FROM clientes_ext WHERE CliExtNumero = {$ViewFra[1]['FraCliNumero']}");
                        $GetNewLast = $GLOBALS['MYT']->db_array("SELECT MAX(FraFechaIngreso) AS FraFechaIngreso FROM facturas WHERE FraCliNumero = {$ViewFra[1]['FraCliNumero']} AND FraStatus NOT IN (1,9,10,21) GROUP BY FraCliNumero");
                        if ($GetNewLast[1]['FraFechaIngreso'] == '' AND $GetExtCli[1]['CliHasArc'] == 1) {
                            $GetNewLast = $GLOBALS['MYT']->db_array("SELECT MAX(FraFechaIngreso) AS FraFechaIngreso FROM officeschema_stored.facturas WHERE FraCliNumero = {$ViewFra[1]['FraCliNumero']} AND FraStatus NOT IN (1,9,10,21) GROUP BY FraCliNumero");
                        }
                        if ($GetNewLast[1]['FraID'] == '') {
                            $GetNewLast[1]['FraID'] = 0;
                        }
                        if ($GetNewLast[1]['FraFechaIngreso'] == '') {
                            $GetNewLast[1]['FraFechaIngreso'] = 'null';
                        } else {
                            $GetNewLast[1]['FraFechaIngreso'] = "'" . $GetNewLast[1]['FraFechaIngreso'] . "'";
                        }
                        $GLOBALS['MYT']->db_query("UPDATE clientes_ext SET CliLastSend = {$GetNewLast[1]['FraFechaIngreso']}, numerofactura = {$GetNewLast[1]['FraID']} WHERE CliExtNumero = {$ViewFra[1]['FraCliNumero']}");
                        if ($ViewFra[1]['FraProm'] > 0) {
                            $ChkCashier = $GLOBALS['MYT']->db_array("SELECT * FROM csh_moviments
							INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
							WHERE cvu_OpType = CorrCashier AND cvu_Type = 5 AND cvu_Office = {$ViewFra[1]['FraOrigen0']} AND cvu_User = {$ViewFra[1]['FraOrigen2']} AND cvu_Status = 1
													AND cvu_Currency = {$ViewFra[1]['FraMonCobro']}
													AND DATE(cvu_Time) = DATE('{$ViewFra[1]['FraFechaIngreso']}')");
                            if (isset($ChkCashier[1]['cvu_ID'])) {
                                $GLOBALS['MYT']->db_query("UPDATE csh_moviments
							INNER JOIN corresponsales ON CorrNumero = {$ViewFra[1]['FraCorresponsal']}
							SET cvu_Amount = cvu_Amount-{$ViewFra[1]['FraProm']}
							WHERE cvu_ID = {$ChkCashier[1]['cvu_ID']}");
                            } else {
                                $GETCASHIER = $GLOBALS['MYT']->db_array("SELECT CorrCashier FROM corresponsales WHERE CorrNumero = {$ViewFra[1]['FraCorresponsal']}");
                                $GLOBALS['MYT']->db_query("INSERT INTO csh_moviments
					(cvu_Type, cvu_User, cvu_Office, cvu_Time, cvu_Status, cvu_POOL, cvu_Amount, cvu_OpType, cvu_Currency, cvu_Deposit) VALUES (5,$_SESSION[$SESSID],$_SESSION[$SESSCD],DATE('{$ViewFra[1]['FraFechaIngreso']}'),1,0,0-{$ViewFra[1]['FraProm']},{$GETCASHIER[1]['CorrCashier']},{$ViewFra[1]['FraMonCobro']},0)");
                            }
                        }
                        $GetBonus = $GLOBALS['MYT']->db_array("SELECT * FROM bonusinvoice WHERE bns_Fra = $RemittanceID AND bns_Type IN (1,7,11)");
                        for ($r = 1; $r <= $GetBonus[0]; $r++) {
                            if ($GetBonus[$r]['CAMPAIGN'] == 2 && $GetBonus[$r]['bns_Type'] == 7) {
                                $GLOBALS['MYT']->db_query("UPDATE unionclientesangariadores SET uca_Flag = 1 WHERE uca_Gather = {$GetBonus[$r]['CLIENT']}");
                            }
                            $MkBalance = make_bnsbalance(0, $GetBonus[$r]['bns_Amount'], $GetBonus[$r]['CLIENT']);
                            $GLOBALS['MYT']->db_query("UPDATE bonusinvoice SET bns_Status = 9 WHERE REF = {$GetBonus[$r]['REF']}");
                            $GLOBALS['MYT']->db_query("INSERT INTO bonusinvoice (INVOICE, CLIENT, CAMPAIGN, bns_Time, bns_User, bns_Amount, bns_Type, bns_CliReference, bns_Origen1, bns_Currency, bns_Value, bns_Product, bns_Status, bns_Partner, bns_Fra, bns_Balance)
			VALUES (0,{$GetBonus[$r]['CLIENT']},1,'$sysTime',$_SESSION[$SESSID],-{$GetBonus[$r]['bns_Amount']},2,0,$_SESSION[$SESSCD],1,-{$GetBonus[$r]['bns_Value']},0,10,0,{$GetBonus[$r]['bns_Fra']},{$MkBalance})");
                        }
                    }
                    hs_invoicestatus($RemittanceID, $sysTime, $PIDCODE, $NewStatus, $ActionObservation, $sysTime);
                } else {
                    $error = 'Erro ao anular.';
                }
            } else {
                echo 'Nao é possivel anular, ordem transmitida.';
            }
        } elseif ($NewStatus == 8) {
            $prcResult = 0;
            #Trocado SP
            $row = tuga_multiquery("CALL do_invoicepayment($RemittanceID,$PIDCODE,$NewStatus,'$ActionObservation',$_SESSION[$SESSID],'$sysTime', @PrcResponse); SELECT @PrcResponse");
            #$row = tuga_multiquery("CALL fn_incidence (@IncidentID,$RemittanceID,'$sysTime',$_SESSION[$SESSID],'{$ActionObservation}',2,'','','0'); SELECT @IncidentID");
            $prcResult = $row[0];
            if ($prcResult > 0) {
                hs_invoicestatus($RemittanceID, $sysTime, $PIDCODE, $NewStatus, $ActionObservation, $sysTime);
            } else {
                $error = 'Erro a registar incidencia.';
            }
        } elseif ($NewStatus == 11) {
            $prcResult = 0;
            $row = tuga_multiquery("CALL do_invoicepayment($RemittanceID,$PIDCODE,{$NewStatus},'$ActionObservation',
			{$_SESSION[$SESSID]},'{$sysTime}', @PrcResponse); SELECT @PrcResponse");
            $prcResult = $row[0];
            $GetValor = $GLOBALS['MYT']->db_array("SELECT FraARecibir FROM facturas WHERE FraID = {$RemittanceID}");
            $GLOBALS['MYT']->db_query("INSERT INTO csh_moviments (cvu_Type, cvu_User, cvu_Office, cvu_Time, cvu_POOL,cvu_Amount,cvu_OpType,cvu_Currency,cvu_Observations) VALUES
		(4,{$_SESSION[$SESSID]},{$_SESSION[$SESSCD]},'$sysTime','0','{$GetValor[1]['FraARecibir']}',1,1,'')");

            if ($prcResult > 0) {
                hs_invoicestatus($RemittanceID, $sysTime, $PIDCODE, $NewStatus, $ActionObservation, $sysTime);
            } else {
                $error = 'Erro no pagamento.';
            }
        } elseif ($NewStatus == 18) {
            $prcResult = 0;
            $row = tuga_multiquery("CALL fn_incidence (@IncidentID,$RemittanceID,'$sysTime',$_SESSION[$SESSID],'$ActionObservation',1,'FraStatus','REG','$NewStatus',1,null); SELECT @IncidentID");
            $prcResult = $row[0];

            if ($prcResult > 0) {
                $row = tuga_multiquery("CALL do_invoicepayment($RemittanceID,$PIDCODE,$NewStatus,'$ActionObservation',$_SESSION[$SESSID],'$sysTime', @PrcResponse); SELECT @PrcResponse");
                if (hs_invoicestatus($RemittanceID, $sysTime, $PIDCODE, $NewStatus, $ActionObservation, $sysTime) == true) {
                    
                } else {
                    $error = 'Erro a pedir cancelamento.';
                }
            } else {
                $error = 'Erro a registar incidencia de cancelamento.';
            }
        }
        if (isset($error)) {
            return array(0, $error);
        } else {
            return array(1, 1);
        }
    }

    function BlockRemittance($remittanceData) {
        global $sysTime, $SESSID;
        if ($remittanceData['blk_STATUS'] == 1) {
            if ($GLOBALS['MYT']->db_query("INSERT INTO hd_bloqueos
	(blk_Fecha, blk_Codigo, lbk_Tipo, lbk_Nivel, blk_Usuario, blk_Informacion, blk_STATUS)
	VALUES
	('$sysTime',{$remittanceData['blk_Codigo']},2,{$remittanceData['FraStatus']},$_SESSION[$SESSID],'{$remittanceData['blk_Information']}',{$remittanceData['blk_STATUS']})") == true) {
                $GLOBALS['MYT']->db_query("UPDATE facturas
			SET FraStatus = 14
			WHERE FraID = {$remittanceData['blk_Codigo']}");
                return $remittanceData['blk_Codigo'];
            } else {
                return 'Erro';
            }
        } elseif ($remittanceData['blk_STATUS'] != '') {
            if ($GLOBALS['MYT']->db_query("UPDATE hd_bloqueos
		SET
		blk_STATUS = 0,
		blk_CloseTime = '$sysTime',
		blk_CloseUser = $_SESSION[$SESSID],
		blk_CloseInformation = '{$_POST['blk_CloseInformation']}'
		WHERE blk_ID = {$remittanceData['blk_ID']}") == true) {
                $GETST = $GLOBALS['MYT']->db_array("SELECT lbk_Nivel FROM hd_bloqueos WHERE blk_ID = {$remittanceData['blk_ID']}");
                $GLOBALS['MYT']->db_query("UPDATE facturas
			SET FraStatus = {$GETST[1]['lbk_Nivel']}
			WHERE FraID = {$remittanceData['blk_Codigo']}");
                return $remittanceData['blk_Codigo'];
            } else {
                return 'Erro';
            }
        }
    }

    function UnblockRemittance() {

    }

    static function ac_number($ob_pre, $ob_num, $ob_sux, $ob_mod, $md) {
        mn_field('text', $md, 'cb_Cuenta_prx', $ob_pre, '2', '3', '', '');
        echo '/';
        mn_field('text', $md, 'cb_Cuenta', $ob_num, '8', '12', '', '');
        echo '-';
        mn_field('text', $md, 'cb_Cuenta_sux', $ob_sux, '1', '2', '', '');
        echo ' md.';
        mn_field('text', $md, 'cb_Cuenta_mod', $ob_mod, '1', '1', 'False', '" style="font-size:8px; border:\'1px solid #006633\'');
    }

}

?>
